from datetime import datetime

from django.db import models
from django.contrib.auth.models import AbstractUser
from django.utils import timezone

from apps.user.models import User


class Difficulty(models.Model):
    """
    题目难度分类
    """
    name = models.CharField(verbose_name='题目难度', max_length=20)
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_difficulty'
        verbose_name = '题目难度'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Label(models.Model):
    """
    题目所在的标签
    """
    name = models.CharField(verbose_name='题目所在的标签', max_length=20)
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')
    create_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
    modify_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    user = models.ForeignKey(User, related_name="userss", verbose_name='出题的用户', on_delete=models.CASCADE)

    class Meta:
        db_table = 'tb_label'
        verbose_name = '题目所在的标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Subject(models.Model):
    """
    题目
    """
    TYPE_CHOICES = (
        ('0', "单选题"),
        ('1', "多选题"),
        ('2', "问答题"),
        ('3', "填空题"),
        ('4', "判断题")
    )
    title = models.CharField(verbose_name='题目', max_length=255)
    type = models.CharField(max_length=6, choices=TYPE_CHOICES, verbose_name="题目类型")
    difficulty = models.ForeignKey(Difficulty, related_name="difficultys", verbose_name='难度', on_delete=models.CASCADE)
    is_deleted = models.BooleanField(default=False, verbose_name='逻辑删除')
    create_time = models.DateTimeField(verbose_name='创建时间', default=timezone.now)
    modify_time = models.DateTimeField(verbose_name='修改时间', auto_now=True)
    label = models.ManyToManyField(Label, related_name='labels', verbose_name='标签')  # 标签
    user = models.ForeignKey(User, related_name="users", verbose_name='出题的用户', on_delete=models.CASCADE)
    analysis = models.CharField(verbose_name='题目的解析', max_length=255)
    answer = models.CharField(verbose_name='题目的答案', max_length=255)  # 填空题答案多对一
    choice_a = models.TextField(verbose_name=u"A选项", default="A.")
    choice_b = models.TextField(verbose_name=u"B选项", default="B.")
    choice_c = models.TextField(verbose_name=u"C选项", default="C.")
    choice_d = models.TextField(verbose_name=u"D选项", default="D.")
    choice_e = models.TextField(verbose_name=u"E选项", default="E.")
    choice_f = models.TextField(verbose_name=u"F选项", default="F.")
    note = models.TextField(verbose_name=u"备注信息", default=u"问答题在此处做答")
    boolt = models.TextField(verbose_name=u"判断正误正确选项", default="对")
    boolf = models.TextField(verbose_name=u"判断正误错误选项", default="错")
    choice_num = models.IntegerField(verbose_name=u"选项数", default=4)

    class Meta:
        db_table = 'tb_subject'
        verbose_name = '题目'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title

